<template>
  <div class="mod-config">
    <el-form :inline="true">
      <el-form-item>
        <date-phase ref="datePhase" dateTypeRange="0234" :datePhase="datePhase" ></date-phase>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getReport()" :loading="dataListLoading">查询</el-button>
        <el-button v-if="(!editMode) && (getCanEdit())" @click="edit()">编辑</el-button>
        <el-button v-if="editMode && (getCanEdit())" :loading="saveLoading" @click="save()">保存</el-button>
        <el-button type="primary" @click="exportExcel">导出</el-button>
      </el-form-item>
    </el-form>
      <el-table :data="reportItems"  id="sale-table">
        <el-table-column
          prop="datePhase"
          label="时间"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="plat"
          label="平台"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="saleValue"
          label="业绩$"
          min-width="100">
        </el-table-column>
        <el-table-column label="店铺" align="center">
          <el-table-column
            prop="shopVisitNum"
            label="访客数"
            min-width="80">
            <template slot-scope="scope">
              <el-input-number size="mini" class="edit-cell" :precision="0" :controls="false" v-if="editMode" v-model="scope.row.shopVisitNum" />
              <span v-if="!editMode">{{scope.row.shopVisitNum}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="shopExposureNum"
            label="曝光数"
            min-width="80">
            <template slot-scope="scope">
              <el-input-number size="mini" class="edit-cell" :precision="0" :controls="false" v-if="editMode" v-model="scope.row.shopExposureNum" />
              <span v-if="!editMode">{{scope.row.shopExposureNum}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="shopClickNum"
            label="点击数"
            min-width="80">
            <template slot-scope="scope">
              <el-input-number size="mini" class="edit-cell" :precision="0" :controls="false" v-if="editMode" v-model="scope.row.shopClickNum" />
              <span v-if="!editMode">{{scope.row.shopClickNum}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="shopClickRate"
            label="点击率%"
            min-width="80">
          </el-table-column>
          <el-table-column
            prop="shopQueryNum"
            label="询盘数"
            min-width="80">
            <template slot-scope="scope">
              <el-input-number size="mini" class="edit-cell" :precision="0" :controls="false" v-if="editMode" v-model="scope.row.shopQueryNum" />
              <span v-if="!editMode">{{scope.row.shopQueryNum}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="shopTmNum"
            label="TM数"
            min-width="80">
            <template slot-scope="scope">
              <el-input-number size="mini" class="edit-cell" :precision="0" :controls="false" v-if="editMode" v-model="scope.row.shopTmNum" />
              <span v-if="!editMode">{{scope.row.shopTmNum}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="shopCpf"
            label="CPF"
            min-width="80">
          </el-table-column>
        </el-table-column>
        <el-table-column label="外贸直通车" align="center">
          <el-table-column
            prop="carExposureNum"
            label="曝光"
            min-width="80">
            <template slot-scope="scope">
              <el-input-number size="mini" class="edit-cell" :precision="0" :controls="false" v-if="editMode" v-model="scope.row.carExposureNum" />
              <span v-if="!editMode">{{scope.row.carExposureNum}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="carClickNum"
            label="点击"
            min-width="80">
            <template slot-scope="scope">
              <el-input-number size="mini" class="edit-cell" :precision="0" :controls="false" v-if="editMode" v-model="scope.row.carClickNum" />
              <span v-if="!editMode">{{scope.row.carClickNum}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="carClickRate"
            label="点击率%"
            min-width="80">
          </el-table-column>
          <el-table-column
            prop="carCost"
            label="花费$"
            min-width="80">
            <template slot-scope="scope">
              <el-input-number size="mini" class="edit-cell" :precision="2" :controls="false" v-if="editMode" v-model="scope.row.carCost" />
              <span v-if="!editMode">{{scope.row.carCost}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="carCpc"
            label="CPC"
            min-width="80">
          </el-table-column>
        </el-table-column>
          <el-table-column
            prop="acos"
            label="ACOS%"
            min-width="80">
          </el-table-column>
      </el-table>
    </div>
</template>

<script>
  import DatePhase from  '@/components/sale/date-phase'
  import dingMixin from  '@/mixins/ding'
  import salePoMixin from  '@/mixins/sale-po'
  import {dateUtil} from '@/utils/dateUtil'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        datePhase:{
            dateType: 2,
            value:'',
            to:''
        },
        editMode: false,
        dataListLoading: false,
        saveLoading:false,
        reportItems:[]
      }
    },
    mixins: [dingMixin, salePoMixin],
    components: {
      DatePhase
    },
    activated () {
      //this.getDataList()
    },
    methods: {
      exportExcel(){
        this.$nextTick(function () {
          let wb = XLSX.utils.table_to_book(document.getElementById('sale-table'));
          let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
          try {
            FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), 'ali.xlsx')
          } catch (e) {
            if (typeof console !== 'undefined') console.log(e, wbout)
          }
          return wbout;
        });
      },
      save(){
        this.saveLoading = true;
        let res = this.checkInputData()
        this.$http({
          url: this.$http.adornUrl('/sale/po/saveAliData'),
          method: 'post',
          data: this.$http.adornData(res,false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.getReport()
          } else {
            this.$message({message: data.msg, type: 'error', duration: 3000})
          }
        }).finally(()=>{
          this.saveLoading = false
          this.editMode=false
        })
      },
      getCanEdit(){
        if(!(this.isPoTeam || this.isSaleLeader || this.isPlatUser(3002, this.userInfo.dingId))){
          return false
        }

        if(this.reportItems.length>0){
          if(dateUtil.nDayExpire(30, this.reportItems[0].createAt) && !this.isSaleLeader) {
            return false;
          }
          return (this.datePhase.dateType==2||this.datePhase.dateType==3) && (this.reportItems[0].dateType==2 || this.reportItems[0].dateType==3)
        }
        return false
      },
      checkInputData(){
        let res = this.reportItems.filter(x=>x.plat!='合计'&&x.plat!='总计')
        return res
      },
      edit(){
        this.editMode=true;
      },
      // 获取数据列表
      getReport () {
        if(this.datePhase.value == null || this.datePhase.value == ''){
          this.$message({message: '请选择时间', type: 'error', duration: 3000})
          return
        }
        this.editMode = false
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sale/po/listAliData'),
          method: 'get',
          params: this.$http.adornParams({
            'datePhase': this.datePhase.value,
            'dateType': this.datePhase.dateType,
            'to': this.datePhase.to
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.reportItems = data.data
          } else {
            this.reportItems = []
          }
        }).finally(()=>{
          this.dataListLoading = false
        })
      }
    }
  }
</script>
<style lang="scss">
  .el-table{
    font-size: 12px;
  }

  .edit-cell{
    width: 60px;
  }

  .el-input-number.is-without-controls .el-input__inner{
    padding: 0;
    text-align: left;
  }

</style>
